﻿using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Content;
using System;

namespace PenguinForce
{
    /// <summary>
    /// WaypointList is a drawable List of screen locations
    /// </summary>
    public class WaypointList : Queue<Vector2>
    {
        #region Variables

        // Draw data
        private Vector2 _waypointCenter;

        private Random _random;
        private List<Texture2D> _fishes;

        private List<int> _textureIndices;

        #endregion

        #region Methods

        /// <summary>
        /// Load the WaypointList's texture resources
        /// </summary>
        /// <param name="content"></param>
        public void LoadContent(ContentManager content)
        {
            _textureIndices = new List<int>();

            _waypointCenter = new Vector2(16, 16);

            _random = new Random();
            _fishes = new List<Texture2D>();
            _fishes.Add(content.Load<Texture2D>("Fish/mackerel"));
            _fishes.Add(content.Load<Texture2D>("Fish/salmon"));
            _fishes.Add(content.Load<Texture2D>("Fish/tuna"));
        }

        public new void Enqueue(Vector2 position)
        {
            base.Enqueue(position);
            _textureIndices.Add(_random.Next(0, _fishes.Count));
        }

        public new Vector2 Dequeue()
        {
            _textureIndices.RemoveAt(0);
            return base.Dequeue();
        }

        /// <summary>
        /// Draw the waypoint list, fading from red for the next waypoint to blue 
        /// for the last
        /// </summary>
        /// <param name="spriteBatch"></param>
        public void Draw(SpriteBatch spriteBatch)
        {
            if (Count == 1)
            {
                // If we only have a single waypoint in the list we don’t have to 
                // worry about creating a nice color gradient, so we just draw it 
                // as completely red here
                spriteBatch.Draw(_fishes[_textureIndices[0]], Peek(), null, Color.White, 0f, _waypointCenter, 1f, SpriteEffects.None, 0f);
            }
            else if (Count > 0)
            {
                // In this case we have more than one waypoint on the list, so we 
                // want to fade smoothly from red for the first and blue for the 
                // last so we can tell visually what order they’re in
                float numberPoints = this.Count - 1;
                float i = 0;

                foreach (Vector2 location in this)
                {
                    // This creates a gradient between 0 for the first waypoint on 
                    // the list and 1 for the last, 0 creates a color that's 
                    // completely red and 1 creates a color that's completely blue
                    spriteBatch.Draw(_fishes[_textureIndices[(int)i]], location, null, Color.White, // new Color(Vector4.Lerp(Color.Red.ToVector4(), Color.Blue.ToVector4(), i / numberPoints)),
                        0f, _waypointCenter, 1f, SpriteEffects.None, 0f);

                    i++;
                }
            }
        }

        #endregion
    }
}